package ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.AbstractListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ColoredList extends JList
	{
		private static Color Default_Color_Selection =  new Color(120,3,12);
		private static Color Default_Color_Checked   =  UIManager.getColor("List.textBackground");
		private static Color Default_Color_Not_Checked = UIManager.getColor("List.textBackground");
		
		Color hellblau = new Color(0,255,255);
		Color gruen = new Color(0,255,0);
		Color rot = new Color(255,0,0);

					
		private List Strings = null;
		private Vector<Color> Colors = null;
		private Color aktuelle_Farbe = null;
		
		private ListSelectionModel listSelectionModel = null;
		private ColoredList THIS = null;
		
		protected List get_Strings(){
			if (Strings == null){
				Strings = new Vector<String>();
			}
			return Strings;
		}
		protected Vector get_Colors(){
			if (Colors == null){
				Colors = new Vector<Color>();
			}
			return Colors;
		}
		protected void set_Strings(List<String> items){
			Strings = items;
		}
		protected void set_Colors(Vector<Color> items){
			Colors = items;
		}
		
		
	    public ColoredList(final List<String> strings, final Vector<Color> couleur){
	    	super(); //<-Konstruiert ne JList mit leerem Model->
	    	THIS = this;
	    	set_Strings(strings); 
	    	set_Colors(couleur); //
	    	this.listSelectionModel = this.getSelectionModel();
	    	init();
	    	this.repaint();
	    	this.setEnabled(true);
	    }
	    
	    
	    public void init()
	    {
	        setModel(new CheckListModel(Strings));
	        setCellRenderer(new CheckListRenderer());
	        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	        this.setFixedCellHeight(18);
	        
	    }

	    // --------- nested classes -----------------------

	    protected class CheckListRenderer extends JLabel implements
	            ListCellRenderer
	    {
	    	
	        public CheckListRenderer()
	        {
	        	setOpaque( true );
	        	setForeground(UIManager.getColor("List.textForeground"));
	        }
	        public Component getListCellRendererComponent(JList list, Object value,
	                int index, boolean isSelected, boolean cellHasFocus)
	        {
	            setText((String)Strings.get(index));
	            setBackground((Color)Colors.get(index));
	            return this;
	        }
	    }
	   
	    @SuppressWarnings("serial")
	    protected class CheckListModel extends AbstractListModel
	    {
	        private CheckListItem[] CLI = null;
	       
	        public CheckListModel(final List<String> Strings)
	        {
	            CLI = new CheckListItem[Strings.size()];
	            for(int i=0; i<Strings.size(); ++i)
	            {
	                CLI[i] = new CheckListItem(Strings.get(i));
	            }
	            listSelectionModel.addListSelectionListener(
                        new ListSelectionListener(){

							@Override
							public void valueChanged(ListSelectionEvent e) {
								ListSelectionModel lsm = (ListSelectionModel)e.getSource();
								if (Colors.get(THIS.getSelectedIndex()) == gruen){
									
								}
								System.out.println(THIS.getSelectedValue());
							}
                        	
                        });

	        }
	       
	        public int getSize()
	        {
	            return CLI.length;
	        }

	        public Object getElementAt(int index)
	        {
	            return CLI[index];
	        }
	        
	    }
	    protected class CheckListItem extends AbstractButton
	    {    	
	    	private String  text = null;

	        public CheckListItem(String text)
	        {
	        	this.text = text;
	        	this.addActionListener(new ActionListener(){

					@Override
					public void actionPerformed(ActionEvent arg0) {
						System.out.println("Es geht");
					}
	        	});
	        }

	        public String getText()
	        {
	            return this.text;
	        }
	    }


}
